package com.ljf.daily.makemoneysoftware.domain.validator;

import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.EnumUtil;
import com.ljf.daily.makemoneysoftware.domain.repository.DailyDatMakeMoneySoftwareRepository;
import com.ljf.daily.makemoneysoftware.dto.command.DailyDatMakeMoneySoftwareCommand;
import com.ljf.daily.makemoneysoftware.enums.DailyDatMakeMoneySoftwareConfigExecuteEnum;
import com.ljf.daily.makemoneysoftware.dto.command.DailyDatMakeMoneySoftwareConfigCommand;
import com.ljf.daily.makemoneysoftware.dto.query.DailyDatMakeMoneySoftwareQuery;
import com.ljf.daily.makemoneysoftware.enums.DailyDatMakeMoneySoftwareConfigAccountLimitModeEnum;
import com.ljf.daily.makemoneysoftware.enums.DailyDatMakeMoneySoftwareConfigAccountModeEnum;
import com.ljf.daily.makemoneysoftware.model.DailyDatMakeMoneySoftware;
import com.ljf.daily.makemoneysoftware.vo.DailyDatMakeMoneySoftwareVO;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 赚钱软件定义  业务校验
 *
 * @author LJF
 * @since 2024-02-20
 */
@Component
public class DailyDatMakeMoneySoftwareValidator extends BasisValidator<DailyDatMakeMoneySoftwareQuery, DailyDatMakeMoneySoftwareCommand, DailyDatMakeMoneySoftwareVO, DailyDatMakeMoneySoftware, DailyDatMakeMoneySoftwareRepository> {

    /**
     * 更新配置 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkEditConfig(DailyDatMakeMoneySoftwareConfigCommand command) throws ServiceException {
        int count = repository.count(DailyDatMakeMoneySoftwareQuery.builder().code(command.getCode()).build());
        if (count == 0) {
            throw new ServiceException("校验异常: 软件未定义");
        }
        if (!EnumUtil.checkCode(command.getExecuteInsert(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 新增执行异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteImport(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 导入执行异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteTemplate(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 模板执行异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteDelete(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 删除执行异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteConsumption(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 消费执行异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostWithhold(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用预扣异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostWithholdLeveling(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用预扣调平异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostConsume(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用消耗异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostConsumeLeveling(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用消耗调平异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostDifference(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用调整异常");
        }
        if (!EnumUtil.checkCode(command.getExecuteCostDifferenceLeveling(), DailyDatMakeMoneySoftwareConfigExecuteEnum.values())) {
            throw new ServiceException("校验异常: 费用调整调平异常");
        }
        DailyDatMakeMoneySoftwareConfigAccountModeEnum accountModeEnum = EnumUtil.getEnumByCode(
                command.getAccountMode(), DailyDatMakeMoneySoftwareConfigAccountModeEnum.values()
        );
        if (accountModeEnum == null) {
            throw new ServiceException("校验异常: 记账模式异常");
        }
        if (Boolean.TRUE.equals(accountModeEnum.getAccountLimit())) {
            if (command.getAccountLimitMode() == null) {
                throw new ServiceException("校验异常: 限数模式不能为空");
            }
            DailyDatMakeMoneySoftwareConfigAccountLimitModeEnum accountLimitModeEnum = EnumUtil.getEnumByCode(
                    command.getAccountLimitMode(), DailyDatMakeMoneySoftwareConfigAccountLimitModeEnum.values()
            );
            if (accountLimitModeEnum == null) {
                throw new ServiceException("校验异常: 限数模式异常");
            }
            boolean checkLimitNum = command.getLimitNum() == null
                    && Objects.equals(command.getAccountLimitMode(), DailyDatMakeMoneySoftwareConfigAccountLimitModeEnum.LIMIT_NUMBER.getCode());
            if (checkLimitNum) {
                throw new ServiceException("校验异常: 限制次数不能为空");
            }
            checkLimitNum = command.getLimitNum() != null
                    && Objects.equals(command.getAccountLimitMode(), DailyDatMakeMoneySoftwareConfigAccountLimitModeEnum.LIMIT_NUMBER.getCode())
                    && command.getLimitNum() < 0;
            if (checkLimitNum) {
                throw new ServiceException("校验异常: 限制次数不能为负数");
            }
        }
    }

    /**
     * 配置分页查询 业务校验
     *
     * @param query
     * @throws ServiceException
     */
    public void checkQueryConfig(DailyDatMakeMoneySoftwareQuery query) throws ServiceException {
        if (query == null) {
            throw new ServiceException("查询异常:查询条件不能为空");
        }
    }

}
